package app.fugema.shop.constants;


import com.base.common.app.BaseConstant;
import com.base.common.utils.DateUtils;
import com.base.common.utils.IOUtils;
import com.base.common.utils.SPUtils;
import com.base.common.utils.UIUtils;
import com.jeremyliao.liveeventbus.LiveEventBus;
import app.fugema.shop.main.login.model.MUserInfo;

public class PublicGlobal {
    public static boolean isWxBinding = false;//是微信绑定

    private static MUserInfo userInfo;

    public static MUserInfo getUserInfo()
    {
        return initUserInfo();
    }

    public static void setUserInfo(MUserInfo userInfo)
    {
        if ( userInfo != null )
        {
            PublicGlobal.userInfo = userInfo;
            SPUtils.putBean(BaseConstant.USER_BEAN, userInfo);
            SPUtils.putInt(BaseConstant.GATEWAY_USERID,userInfo.getUserId());
        }
    }

    public static MUserInfo initUserInfo()
    {
        if (userInfo == null)
        {
            userInfo = SPUtils.getBean(BaseConstant.USER_BEAN, MUserInfo.class);
        }
        return userInfo;
    }

    private static String deviceToken;
    public static String getDeviceToken()
    {
        if (UIUtils.isEmpty(deviceToken))
        {
            deviceToken = SPUtils.getString(AppConstants.DEVICE_TOKEN,"");
        }
        return deviceToken;
    }
    public static void setDeviceToken(String mDeviceToken)
    {
        if(UIUtils.isNotEmpty( mDeviceToken ))
        {
           deviceToken = mDeviceToken;
            SPUtils.putBean(AppConstants.DEVICE_TOKEN, mDeviceToken);
        }
    }

    /**
     * @param page -1,不跳转  0商城  1
     */
    public static void getToHomePage(int page)
    {
        LiveEventBus.get(AppConstants.EventKey.GO_TO_HOME_PAGE, Integer.class).post(page);
    }



    /**
     * 时间的转换规则
     *
     * @param time
     * @return
     */
    public static String getDate(long time, long... nowTime) {
        long now = System.currentTimeMillis();
        if (nowTime.length > 0 && nowTime[0] > 0) now = nowTime[0];
        //秒数
        long seconds = Math.abs(DateUtils.getSubtractTimeInMillis(time, now) / 1000);
        String date = "";

        if (seconds >= 0) {
            if (seconds < 60) {
                date = "刚刚";
            } else if (seconds < 60 * 60) {
                date = (seconds / 60) + "分钟前";
            } else if (seconds < 60 * 60 * 24) {
                date = (seconds / 3600) + "小时前";
            } else {
                int days = DateUtils.getSubtractDay(time, now, true);
                if (days == 1) {
                    date = "昨天";
                } else if (days < 8) {
                    date = days + "天前";
                } else {
                    date = DateUtils.dateLongToString(time, "MM月dd日");
                }
            }
        } else {
            date = DateUtils.dateLongToString(time, "MM月dd日");
        }
        return date;
    }


}
